<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="plus">新增角色</el-button>
    </div>

    <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">

      <el-table-column align="center" label="序号" width="65">
        <template scope="scope">
          <span>{{scope.row.Id}}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="300px" label="角色名">
        <template scope="scope">
          <el-input v-show="scope.row.edit" size="small" v-model="scope.row.SrName"></el-input>
          <span v-show="!scope.row.edit">{{ scope.row.SrName }}</span>
        </template>
      </el-table-column>


      <el-table-column min-width="300px" label="备注">
        <template scope="scope">
          <el-input v-show="scope.row.edit" size="small" v-model="scope.row.SrMemo"></el-input>
          <span v-show="!scope.row.edit">{{ scope.row.SrMemo }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template scope="scope">
          <el-button v-show='!scope.row.edit' type="primary" @click='scope.row.edit=true' size="small" icon="edit">编辑</el-button>
          <el-button v-show='scope.row.edit' type="success" @click='handleUpdate(scope.row)' size="small" icon="check">完成</el-button>
          <el-button  size="small" type="danger" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>


    <el-dialog :title="tempTitle" :visible.sync="dialogFormVisible">
      <el-form class="small-space" :model="temp" label-position="left" label-width="70px" style='width: 400px; margin-left:50px;'>


        <el-form-item label="角色名">
          <el-input v-model="temp.SrName"></el-input>
        </el-form-item>


        <el-form-item label="备注">
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入内容" v-model="temp.SrMemo">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="create">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import { addRole, delRole, updateRole, fetchList } from 'api/system_role';


  export default {
    name: 'system_role',
    data() {
      return {
        list: null,
        total: null,
        listLoading: true,
        temp: {
          SrName: '',
          SrMemo: '',
        },
        tempTitle: '新增角色',
        dialogFormVisible: false,
        tableKey: 0
      }
    },
    created() {
      this.getList();
    },

    methods: {
      getList() {
        this.listLoading = true;
        fetchList().then(response => {
          this.list = response.data.map(v => {
            v.edit = false;
            return v
          });
          this.total = response.data.total;
          this.listLoading = false;
        })
      },
      handleCreate() {
        this.resetTemp();
        this.dialogStatus = 'create';
        this.dialogFormVisible = true;
      },
      handleUpdate(row) {
        updateRole(row).then(response => {
          if (response.data === 'OK') {
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            });
            row.edit = false;
          } else {
            this.$notify({
              title: '失败',
              message: response.data,
              type: 'error',
              duration: 2000
            });
          }
        })
      },
      handleDelete(row) {
        delRole(row.Id).then(response => {
          if (response.data === 'OK') {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            });
            const index = this.list.indexOf(row);
            this.list.splice(index, 1);
          } else {
            this.$notify({
              title: '失败',
              message: response.data,
              type: 'error',
              duration: 2000
            });
          }
        })
      },
      create() {
        if (this.temp.SrName === '') {
          this.$message({
            message: '角色名必填',
            type: 'error'
          });
          return ;
        } else {
          addRole(this.temp).then(response => {
            if (response.data === 'OK') {
              this.$notify({
                title: '成功',
                message: '新增角色成功',
                type: 'success',
                duration: 2000
              });
              this.dialogFormVisible = false;
              this.list.unshift(this.temp);
            } else {
              this.$notify({
                title: '失败',
                message: response.data,
                type: 'error',
                duration: 2000
              });
            }
          })
        }
      },
      update() {
        this.temp.timestamp = +this.temp.timestamp;
        for (const v of this.list) {
          if (v.id === this.temp.id) {
            const index = this.list.indexOf(v);
            this.list.splice(index, 1, this.temp);
            break;
          }
        }
        this.dialogFormVisible = false;
        this.$notify({
          title: '成功',
          message: '更新成功',
          type: 'success',
          duration: 2000
        });
      },
      resetTemp() {
        this.temp = {
          SrName: '',
          SrMemo: '',
        };
      },

    }
  }
</script>
